package com.util;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther
 * @date 2019/8/6 15:02
 */
public class StringUtil {

    private StringUtil() {
    }

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isBlank(final CharSequence cs) {
        return isEmpty(cs);
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

    public static String buildUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static boolean contains(String str, String searchStr) {
        if (str != null && searchStr != null) {
            return str.indexOf(searchStr) >= 0;
        } else {
            return false;
        }
    }

    public static boolean contains(String str, char searchChar) {
        if (isEmpty(str)) {
            return false;
        } else {
            return str.indexOf(searchChar) >= 0;
        }
    }

    public static boolean equals(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equals(str2);
    }

    public static String createTanquId() {
        int i = 100000000 + new Random().nextInt(899999999);
        return String.valueOf(i);
    }


    /**
     * @Author chl
     * @Description 加法运算
     * @Date 18:02 2021/5/7
     * @param a a
     * @param b b
     * @return response response
     **/
    public static String addOperation(String a, String b) {
        char [] axArrs = a.toCharArray();

        char [] bxArrs = b.toCharArray();

        char [] tMax = axArrs.length> bxArrs.length?axArrs:bxArrs;//max

        char [] tMin = axArrs.length> bxArrs.length?bxArrs:axArrs;//min

        int len = tMax.length - tMin.length;

        char [] tArrAppend = new char[len];

        char [] tMin2 = new char[tMin.length];

        System.arraycopy(tMax, 0, tArrAppend, 0, len);

        System.arraycopy(tMax, len, tMin2, 0, tMin2.length);

        int [] tMin3 = new int[tMin.length];

        boolean flag = false;

        for (int i=tMin.length-1;i>-1;i--){
            int count = Character.getNumericValue(tMin2[i])+Character.getNumericValue(tMin[i]) + (flag?1:0);

            int b1 = count;

            flag = count>9;

            if (flag){
                b1 = count % 10;
            }

            tMin3[i]= b1;
        }

        int [] t = new int[tArrAppend.length];

        for (int i=tArrAppend.length-1;i>-1;i--){
            int count = Character.getNumericValue(tArrAppend[i])+ (flag?1:0);

            int b1 = count;

            flag = count>9;

            if (flag){
                b1 = count % 10;

            }

            t[i] = b1;

        }

        int [] result = new int[tMax.length+ (flag?1:0)];

        if (flag){
            result[0] = 1;

        }
        System.arraycopy(t,0,result,(flag?1:0),t.length);

        System.arraycopy(tMin3,0,result,t.length+(flag?1:0),tMin3.length);

        return Arrays.toString(result).replaceAll("[\\[,\\]\\s*]", "");
    }

    /**
     * 根据输入量里面获取内容
     *
     * @param bufferedReader
     *
     * @return
     *
     * @throws IOException
     */
    public static String getCallBackContent(BufferedReader bufferedReader) throws IOException {
        StringBuffer contentBuffer = new StringBuffer();
        String line = null;
        while (!StringUtils.isBlank((line = bufferedReader.readLine()))) {
            contentBuffer.append(line);
        }
        return contentBuffer.toString();
    }

    /**
     * 隐藏手机号中间4位
     *
     * @param mobile
     *
     * @return
     */
    public static String halfMobileCover(String mobile) {
        if (isBlank(mobile)) {
            return "";
        }
        if (mobile.length() != 11) {
            return mobile;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(mobile.substring(0, 3));
        sb.append("****");
        sb.append(mobile.substring(7));
        return sb.toString();
    }

    /**
     * 隐藏身份证中间
     *
     * @param idcard
     *
     * @return
     */
    public static String halfIDCardCover(String idcard) {
        if (isBlank(idcard)) {
            return "";
        }
        if (idcard.length() < 9) {
            return halfNameCover(idcard);
        }
        StringBuilder sb = new StringBuilder(idcard);
        String s = "*";
        for (int i = 0; i < idcard.length() - 3; i++) {
            if (i > 4) {
                sb.replace(i - 1, i, s);
            }
        }
        return sb.toString();
    }

    /**
     * 隐藏名字中间
     *
     * @param name
     *
     * @return
     */
    public static String halfNameCover(String name) {
        if (isBlank(name)) {
            return "";
        }
        if (name.length() == 1) {
            return name;
        }

        StringBuilder sb = new StringBuilder(name);
        String s = "*";
        for (int i = 1; i < name.length(); i++) {
            sb.replace(i - 1, i, s);
        }

        return sb.toString();
    }

    /**
     * 判断字符串中是否包含中文
     *
     * @param str 待校验字符串
     *
     * @return 是否为中文
     *
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * @param str str
     *
     * @return response response
     *
     * @Author chl
     * @Description 判断是否手机号
     * @Date 15:53 2021/3/18
     **/
    public static boolean isPhone(String str) {
        String regEx = "^(\\+?86-?)?1(3|4|7|5|8)[0-9]{9}$";
        Pattern compile = Pattern.compile(regEx);
        Matcher m = compile.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 是否是数字
     * <pre>
     * StringUtil.isNumeric(null)   = false
     * StringUtil.isNumeric("")     = false
     * StringUtil.isNumeric("  ")   = false
     * StringUtil.isNumeric("12 3") = false
     * StringUtil.isNumeric("ab2c") = false
     * StringUtil.isNumeric("12-3") = false
     * StringUtil.isNumeric("12.3.3") = false
     * StringUtil.isNumeric("123")  = true
     * StringUtil.isNumeric("-12.3") = true
     * StringUtil.isNumeric("+12.3") = true
     * StringUtil.isNumeric("00000.3") = true
     * StringUtil.isNumeric("00000.") = true
     * </pre>
     *
     * @param str
     *
     * @return
     */
    public static boolean isNumeric(String str) {
        String regEx = "[-\\+]??[0-9]+?\\.??[0-9]*?";
        Pattern compile = Pattern.compile(regEx);
        Matcher m = compile.matcher(str);
        if (m.matches()) {
            return true;
        }
        return false;
    }
}
